#!/bin/bash
j_script_dir=`dirname $0`
j_jenny_root_dir=`(cd $j_script_dir/..; pwd)`

. $j_jenny_root_dir/common/function


usage() {
	echo "Usage: $0 [options]..."
	echo "options:"
	echo "    -c|--config   <debug|release>     	debug is default"
	if [ $j_os_name = "mac" ]; then
		echo "    -t|--toolset  <xcode|make>			xcode is default"
		echo "    -p|--platform  <mac|ios>				mac is default"
	fi
	echo "    -a|--architecture  <x64|x86>				x64 is default"
	echo "    -d|--target  							default is empty, means all"
	echo "	  -s								turn on code signing. default is NO"
	echo "    -h|--help"
	exit 1
}

if [ "$1" = "-h" ]; then 
	usage
fi

detect_cmake j_cmake
j_code_signing=NO
j_code_sign_identity=

parse_args $@

j_defines="-DPROJECT_FILE=$j_config_path -DCMAKE_BUILD_TYPE=$j_config -DJ_TARGET_PLATFORM=$j_platform  -DJ_ARCHITECTURE=$j_architecture -DJ_TOOLSET=$j_toolset -DJ_TOOLSET_VER=$j_toolset_ver"

output=`$j_cmake $j_defines -P $j_jenny_root_dir/cmake/prepare.cmake 2>&1 || die "prepare failed" | grep "="`
for i in $output ; do eval ${i##--}; done

[ -d $j_generated_project_dir ] || die "Failed to locate generated project dir: $j_generated_project_dir"

cd $j_generated_project_dir

if [ $j_generator = "xcode" ]; then
	j_cmake_generator=-G\ Xcode
else
	j_cmake_generator=""
fi
$j_cmake $j_defines $j_cmake_generator  $j_jenny_root_dir/cmake || die "Generate failed"

echo "Generate done."
